##########
# R Info #
##########
version
#platform       x86_64-w64-mingw32          
#arch           x86_64                      
#os             mingw32                     
#system         x86_64, mingw32             
#status                                     
#major          3                           
#minor          6.0                         
#year           2019                        
#month          04                          
#day            26                          
#svn rev        76424                       
#language       R                           
#version.string R version 3.6.0 (2019-04-26)
#nickname       Planting of a Tree 

library(ggplot2)
library(ggpubr)
library(maps)
library(wesanderson)
library(stargazer)
library(dplyr)

sessionInfo()
#R version 3.6.0 (2019-04-26)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19041)
#Matrix products: default
#locale:
#[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
#[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
#[5] LC_TIME=English_United States.1252    
#attached base packages:
#[1] stats     graphics  grDevices utils     datasets  methods   base     
#other attached packages:
#[1] dplyr_1.0.4       stargazer_5.2.2   wesanderson_0.3.6 maps_3.3.0        ggpubr_0.4.0     
#[6] ggplot2_3.3.2    
#loaded via a namespace (and not attached):
#[1] zip_2.0.4         Rcpp_1.0.3        cellranger_1.1.0  pillar_1.4.3      compiler_3.6.0   
#[6] forcats_0.5.0     tools_3.6.0       lifecycle_0.2.0   tibble_3.0.4      gtable_0.3.0     
#[11] pkgconfig_2.0.3   rlang_0.4.10      openxlsx_4.1.4    DBI_1.1.0         rstudioapi_0.11  
#[16] curl_4.3          haven_2.2.0       rio_0.5.16        rJava_0.9-11      withr_2.4.1      
#[21] dplyr_1.0.4       hms_0.5.3         generics_0.0.2    xlsxjars_0.6.1    vctrs_0.3.6      
#[26] grid_3.6.0        tidyselect_1.1.0  data.table_1.12.8 glue_1.4.1        R6_2.4.1         
#[31] rstatix_0.6.0     readxl_1.3.1      foreign_0.8-71    carData_3.0-3     car_3.0-7        
#[36] purrr_0.3.3       tidyr_1.1.2       magrittr_1.5      backports_1.1.5   scales_1.1.0     
#[41] ellipsis_0.3.0    abind_1.4-5       assertthat_0.2.1  colorspace_1.4-1  ggsignif_0.6.0   
#[46] xlsx_0.6.1        stringi_1.4.5     munsell_0.5.0     broom_0.7.5       crayon_1.3.4




#############
# Load Data #
#############
load("PostDataJQD.RData")

colnames(df)
# country:          country name
# year:             year of posting
# yearmonth:        year + month of posting
# party_country:    party name + country name
# party:            party name
# cmp_familyid:     party family id in the CMP
# cmp_family:       party family name in the CMP
# ches_ideology:    general left-right ideology in the CHES
# gps_populism:     populism score in the GPS
# gps_econlr:       economic left-right ideology in the GPS      
# gps_soclr:        social left-right ideology in the GPS
# Page.Name:        name of facebook page
# Likes.at.Posting: number of page likes at posting
# Likes:            number of Likes
# Comments:         number of comments
# Shares:           number of shares
# Love:             number of Love reactions
# Wow:              number of Wow reactions
# Haha:             number of Haha reactions 
# Sad:              number of Sad reactions
# Angry:            number of Angry reactions
# totalreactions:   sum of Likes, Love, Wow, Haha, Sad, and Angry
# loveprop:         proportion of Love reactions (Love / totalreactions)
# angryprop:        proportion of Angry reactions (Angry / totalreactions)

summary(df)

# number of countries
length(unique(df$country)) # 79

# number of parties
length(unique(df$party_country)) # 690

# compute emotional polarization
df$lapolarization <- (df$angryprop + 1) / (df$loveprop + 1)

# increase memory limit
memory.limit(500000000)

# set colors
rgy <- wes_palette("Darjeeling1", 3)




##########################
# Figure 5: Party Family #
##########################
length(which(!is.na(df$cmp_familyid))) # 1049960
length(unique(df$party_country[which(!is.na(df$cmp_familyid))])) # 337
length(unique(df$country[which(!is.na(df$cmp_familyid))])) # 49

# create party family data
partyfamily <- unique(df[,c("cmp_familyid", "cmp_family")])
partyfamily <- partyfamily[-1,]
partyfamily <- partyfamily[order(partyfamily$cmp_familyid),]

partyfamily$`Party Family` <- factor(partyfamily$cmp_family, 
                                     levels=partyfamily$cmp_family)

partyfamily <- rbind(partyfamily, partyfamily)

partyfamily$Reaction <- rep(c("Love", "Angry"), each=10)
partyfamily$Reaction <- factor(partyfamily$Reaction, levels=c("Love", "Angry"))

# calculate average love and angry proportions by party family
partyfamily$prop[1:10] <- tapply(df$loveprop, df$cmp_familyid, mean)
partyfamily$prop[11:20] <- tapply(df$angryprop, df$cmp_familyid, mean)

partyfamily$xloc <- 1:10

partyfamily$type <- "Average Love and Angry Proportions"

partyfamily2 <- cbind(partyfamily[1:10,1:5], partyfamily[11:20,5])
colnames(partyfamily2)[5:6] <- c("loveprop", "angryprop")

partyfamily2$xloc <- 1:10

# figure 5 left panel
party_diff <- ggplot(data = partyfamily, 
                     aes(x = xloc, y = prop, colour = Reaction)) + 
  facet_wrap(. ~ type, ncol = 1) +
  geom_segment(data = partyfamily2, aes(x = xloc, xend = xloc, y = loveprop, yend = angryprop),
               lwd=0.8, color = "gray60") +
  geom_point(cex=4, pch=19, alpha=0.7) +
  scale_color_manual(breaks = c("Love", "Angry"), 
                     values = rgy[2:1]) +
  scale_x_discrete(limits=factor(1:10), labels= partyfamily$cmp_family[1:10]) + 
  xlab("") +
  ylab("Love and Angry Proportion") +
  coord_cartesian(ylim=c(0, 0.072)) +
  theme_bw() +
  theme(axis.text.x=element_text(color = "black", size=11, angle=30, vjust=.8, hjust=0.8),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        legend.title = element_text(size =13),
        legend.text = element_text(size =13),
        legend.position = c(0.12, 0.12),
        legend.background = element_rect(size=0.7, linetype="solid", colour="black"))


# calculate average emotional polarization by party
partyfamily2$lapolarization <- (partyfamily2$angryprop + 1) / (partyfamily2$loveprop + 1)

# reorder dataset
partyfamily2 <- partyfamily2[order(partyfamily2$lapolarization, decreasing=FALSE),]
partyfamily2$xloc <- 1:10

partyfamily2$type <- "Emotional Polarization"

# figure 5 right panel
party_ep <- ggplot(data = partyfamily2, 
                   aes(x = xloc, y = lapolarization)) + 
  geom_hline(yintercept=1, lty=2) +
  geom_line(color = "gray60", lwd=0.8) +
  geom_point(cex=6, pch=18, colour=rgy[3]) +
  facet_wrap(. ~ type, ncol = 1) +
  scale_x_discrete(limits=factor(1:10), labels= partyfamily2$cmp_family) + 
  xlab("") +
  ylab("Emotional Polarization") +
  theme_bw() +
  theme(axis.text.x=element_text(color = "black", size=11, angle=30, vjust=.8, hjust=0.8),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        legend.title = element_text(size =13),
        legend.text = element_text(size =13))


# figure 5 all
ggarrange(party_diff, party_ep,
          ncol = 2, nrow = 1)
#ggsave("PartyFamily2.pdf", width=12, height=6)




######################
# Figure 6: Ideology #
######################
# set ggplot theme
plain2 <- theme(
  panel.border = element_rect(fill = NA),
  panel.grid =  element_line(colour = "grey92"),
  strip.background = element_rect(fill = "grey85", 
                                  colour = "grey20"),
  panel.background = element_rect(fill = "white"),
  plot.title = element_text(hjust = 0, face = "bold"),
  legend.position = "none",
  axis.title.x = element_text(size = 15),
  axis.title.y = element_text(size = 15)
)

# CHES general ideology
length(which(!is.na(df$ches_ideology))) # 795932
length(unique(df$party_country[which(!is.na(df$ches_ideology))])) # 244
length(unique(df$country[which(!is.na(df$ches_ideology))])) # 31

# get random sample and create dataset
set.seed(123456)
psamples <- sample(which(!is.na(df$ches_ideology)), 20000)

il <- df[psamples, c("ches_ideology", "loveprop")]
colnames(il)[2] <- 'prop'
il$type <- "Love"

ia <- df[psamples, c("ches_ideology", "angryprop")]
colnames(ia)[2] <- "prop"
ia$type <- "Angry"

ip <- df[psamples, c("ches_ideology", "lapolarization")]
colnames(ip)[2] <- "prop"
ip$type <- "Emotional Polarization"

df2 <- rbind(il, ia, ip)

df2$type <- factor(df2$type, levels=c("Love", "Angry", "Emotional Polarization"))

blank_data <- data.frame(type=rep(unique(df2$type), each=2), 
                         ches_ideology=0, 
                         prop=c(0,0.11,0,0.11,0.98,1.05))
df2$rugloc <- rep(c(0,0,0.98), each=nrow(df2)/3)

# figure 6 top panel
ches <- ggplot() +
  geom_blank(data = blank_data, aes(x = ches_ideology, y = prop)) +
  geom_hline(data = data.frame(yint=1, type="Emotional Polarization"), 
             aes(yintercept = yint), lty=2) +
  geom_smooth(data = df2, 
              aes(x = ches_ideology, y = prop, colour = type),
              method = "loess", lwd = 1.2,
              level=0.99) +
  facet_wrap(. ~ type, ncol = 3, scales = "free") +
  scale_color_manual(breaks = c("Love", "Angry", "Emotional Polarization"), 
                     values = rgy[c(2:1,3)]) +
  ggtitle(label = "A. CHES General Ideology") +
  xlab("Left---Right") + 
  ylab("") +
  plain2 + 
  scale_y_continuous(breaks = seq(0,2,by=0.03))


# GPS economic ideology
length(which(!is.na(df$gps_econlr))) # 1441299
length(unique(df$party_country[which(!is.na(df$gps_econlr))])) # 424
length(unique(df$country[which(!is.na(df$gps_econlr))])) # 77

# get random sample and create dataset
set.seed(123456)
psamples <- sample(which(!is.na(df$gps_econlr)), 20000)

el <- df[psamples, c("gps_econlr", "loveprop")]
colnames(el)[2] <- 'prop'
el$type <- "Love"

ea <- df[psamples, c("gps_econlr", "angryprop")]
colnames(ea)[2] <- "prop"
ea$type <- "Angry"

ep <- df[psamples, c("gps_econlr", "lapolarization")]
colnames(ep)[2] <- "prop"
ep$type <- "Emotional Polarization"

df2 <- rbind(el, ea, ep)

df2$type <- factor(df2$type, levels=c("Love", "Angry", "Emotional Polarization"))

blank_data <- data.frame(type=rep(unique(df2$type), each=2), 
                         gps_econlr=0, 
                         prop=c(0,0.095,0,0.095,0.92,1.03))
df2$rugloc <- rep(c(0,0,0.92), each=nrow(df2)/3)

# figure 6 middle panel
econ <- ggplot() +
  geom_blank(data = blank_data, aes(x = gps_econlr, y = prop)) +
  geom_hline(data = data.frame(yint=1, type="Emotional Polarization"), 
             aes(yintercept = yint), lty=2) +
  geom_smooth(data = df2, 
              aes(x = gps_econlr, y = prop, colour = type),
              method = "loess", lwd = 1.2,
              level=0.99) +
  facet_wrap(. ~ type, ncol = 3, scales = "free") +
  scale_color_manual(breaks = c("Love", "Angry", "Emotional Polarization"), 
                     values = rgy[c(2:1,3)]) +
  ggtitle(label = "B. GPS Economic Ideology") +
  xlab("Pro State---Pro Market") + 
  ylab("Love and Angry Proportion") +
  plain2 + 
  scale_y_continuous(breaks = seq(0,2,by=0.03))


# GPS social ideology
length(which(!is.na(df$gps_soclr))) # 1472337
length(unique(df$party_country[which(!is.na(df$gps_soclr))])) # 429
length(unique(df$country[which(!is.na(df$gps_soclr))])) # 49

# get random sample and create dataset
set.seed(123456)
psamples <- sample(which(!is.na(df$gps_soclr)), 20000)

sl <- df[psamples, c("gps_soclr", "loveprop")]
colnames(sl)[2] <- 'prop'
sl$type <- "Love"

sa <- df[psamples, c("gps_soclr", "angryprop")]
colnames(sa)[2] <- "prop"
sa$type <- "Angry"

sp <- df[psamples, c("gps_soclr", "lapolarization")]
colnames(sp)[2] <- "prop"
sp$type <- "Emotional Polarization"

df2 <- rbind(sl, sa, sp)

df2$type <- factor(df2$type, levels=c("Love", "Angry", "Emotional Polarization"))

blank_data <- data.frame(type=rep(unique(df2$type), each=2), 
                         gps_soclr=0, 
                         prop=c(0,0.07,0,0.07,0.98,1.02))
df2$rugloc <- rep(c(0,0,0.98), each=nrow(df2)/3)

# figure 6 bottom panel
soc <- ggplot() +
  geom_blank(data = blank_data, aes(x = gps_soclr, y = prop)) +
  geom_hline(data = data.frame(yint=1, type="Emotional Polarization"), 
             aes(yintercept = yint), lty=2) +
  geom_smooth(data = df2, 
              aes(x = gps_soclr, y = prop, colour = type),
              method = "loess", lwd = 1.2,
              level=0.99) +
  facet_wrap(. ~ type, ncol = 3, scales = "free") +
  scale_color_manual(breaks = c("Love", "Angry", "Emotional Polarization"), 
                     values = rgy[c(2:1,3)]) +
  ggtitle(label = "C. GPS Social Ideology") +
  xlab("Liberal---Conservative") + 
  ylab("") +
  plain2 + 
  scale_y_continuous(breaks = seq(0,2,by=0.03))


# figure 6 all
ggarrange(ches, econ, soc,
          ncol = 1, nrow = 3)
#ggsave("Ideology3.pdf", width=12, height=12)




######################
# Figure 7: Populism #
######################
# set ggplot theme
plain2 <- theme(
  panel.border = element_rect(fill = NA),
  panel.grid =  element_line(colour = "grey92"),
  strip.background = element_rect(fill = "grey85", 
                                  colour = "grey20"),
  panel.background = element_rect(fill = "white"),
  plot.title = element_text(hjust = 0, face = "bold"),
  legend.position = "none",
  axis.title.x = element_text(size = 15),
  axis.title.y = element_text(size = 15)
)

# GPS populism score
length(which(!is.na(df$gps_populism))) # 1418011
length(unique(df$party_country[which(!is.na(df$gps_populism))])) # 425
length(unique(df$country[which(!is.na(df$gps_populism))])) # 77

# get random sample and create dataset
set.seed(123456)
psamples <- sample(which(!is.na(df$gps_populism)), 20000)

pl <- df[psamples, c("gps_populism", "loveprop")]
colnames(pl)[2] <- 'prop'
pl$type <- "Love"

pa <- df[psamples, c("gps_populism", "angryprop")]
colnames(pa)[2] <- "prop"
pa$type <- "Angry"

pp <- df[psamples, c("gps_populism", "lapolarization")]
colnames(pp)[2] <- "prop"
pp$type <- "Emotional Polarization"

df2 <- rbind(pl, pa, pp)

df2$type <- factor(df2$type, levels=c("Love", "Angry", "Emotional Polarization"))

blank_data <- data.frame(type=rep(unique(df2$type), each=2), 
                         gps_populism=0, 
                         prop=c(-0.005,0.08,-0.005,0.08,0.97,1.04))
df2$rugloc <- rep(c(-0.005,-0.005,0.97), each=nrow(df2)/3)

# figure 7
ggplot() +
  geom_blank(data = blank_data, aes(x = gps_populism, y = prop)) +
  geom_hline(data = data.frame(yint=1, type="Emotional Polarization"), 
             aes(yintercept = yint), lty=2) +
  geom_smooth(data = df2, 
              aes(x = gps_populism, y = prop, colour = type),
              method = "loess", lwd = 1.2,
              level=0.99) +
  facet_wrap(. ~ type, ncol = 3, scales = "free") +
  scale_color_manual(breaks = c("Love", "Angry", "Emotional Polarization"), 
                     values = rgy[c(2:1,3)]) +
  xlab("Populism") + 
  ylab("Love and Angry Proportion") +
  plain2 + 
  scale_y_continuous(breaks = seq(0,2,by=0.02))
#ggsave("Populism2.pdf", width=12, height=5)
